package minijvm.cmd;

import minijvm.clz.ClassFile;
import minijvm.constant.ClassInfo;
import minijvm.engine.ExecutionResult;
import minijvm.engine.Heap;
import minijvm.engine.JavaObject;
import minijvm.engine.StackFrame;

/**
 * 创建一个对象
 *
 * @author ChangLiang
 * @date 2020/4/26
 */
public class NewObjectCmd extends TwoOperandCmd {

    public NewObjectCmd(ClassFile clzFile, String opCode) {
        super(clzFile, opCode);
    }

    @Override
    public void execute(StackFrame frame, ExecutionResult result) {
        int index = this.getIndex();
        // 从常量池中拿到ClassInfo
        ClassInfo info = (ClassInfo) this.getConstantInfo(index);
        String className = info.getClassName();
        // 在java堆上创建一个实例
        JavaObject javaObject = Heap.getInstance().newObject(className);
        frame.getOperandStack().push(javaObject);
    }
}
